package com.xuan.mail.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuan.mail.domin.Email;
import com.xuan.mail.domin.User;
import com.xuan.mail.service.UserService;
import com.xuan.mail.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

/**
* @author 86156
* @description 针对表【user】的数据库操作Service实现
* @createDate 2024-03-17 13:14:39
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Resource
    private UserMapper userMapper;
    @Resource
    private SshServiceImpl sshServiceImpl;


    @Override
    public String register(User user) {
        // 判断用户名是否已存在
        User existuser = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUserName, user.getUserName())
                .eq(User::getIsdeleted,1)
        );
        User existuserl = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUserName, user.getUserName())
                .eq(User::getIsdeleted,0)
        );

        if(existuserl!=null){
            System.out.printf("用户名已存在，请重新注册");
            return "fail";

        }
        if(existuser!=null){
            userMapper.update(user,new LambdaUpdateWrapper<User>()
                    .eq(User::getUserId,existuser.getUserId())
                    .set(User::getIsdeleted,0)
                    .set(User::getUserPass,user.getUserPass())
            );
            System.out.printf("用户名已存在，但已注销，已重新注册");
            return "success";
        }else if(existuser==null&&existuserl==null) {
            user.setIsAction(1);
            user.setPermissions(1);
            user.setIsdeleted(0);
            user.setIsRemember(0);
            String domain = "xuanyuanchumo.top";
            String mail= user.getUserName()+"@"+domain;
            user.setUserMail(mail);
            super.save(user);
            System.out.printf("注册成功，用户名为"+user.getUserName());
            return "success";

        }else {
            System.out.printf("注册失败");
            return "fail";
        }
    }

    //用户删除
    @Override
    public String logicDeleteUser(User user) {
        userMapper.update(user,new LambdaUpdateWrapper<User>()
                .eq(User::getUserId,user.getUserId())
                .set(User::getIsdeleted,1)
        );
        return "success";
    }
    //修改密码
    @Override
    public String changePassword(User user) {
        //查找当前用户对应id
        userMapper.update(user,new LambdaUpdateWrapper<User>()
                .eq(User::getUserId,user.getUserId())
                .set(User::getUserPass,user.getUserPass())
        );
        return "success";
    }

    @Override
    public boolean isExist(String userName) {
        User user = super.getOne(new LambdaQueryWrapper<User>().eq(User::getUserName, userName));

        return user != null;
    }

    @Override
    public User login(User user) {
        System.out.printf("登录前："+user);

        User userlogin = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUserName,user.getUserName())
                .eq(User::getUserPass,user.getUserPass())
                .eq(User::getIsdeleted,0)
        );

        System.out.printf("登录中："+userlogin);

        //判断用户权限
        if(userlogin.getPermissions()==2&&userlogin.getIsAction()==1){
            //管理员登录

                System.out.printf("邮箱管理员登录成功");
                return userlogin;
        }else if(userlogin.getPermissions()==1&&userlogin.getIsAction()==1){
            //普通用户登录
                //更新登录时间

                    System.out.printf("邮箱用户登录成功");
                    return userlogin;
        }else {
            System.out.printf("登录失败");
            return null;
        }

    }
    //用户是否存在
    @Override
    public User isExistUser(User user) {

        return userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUserName,user.getUserName())
                .eq(User::getUserPass,user.getUserPass())
                .eq(User::getIsdeleted,0)
        );
    }
    //所有用户
    @Override
    public List<User> getAllUser() {
        return userMapper.selectList(new LambdaQueryWrapper<User>()
                .eq(User::getIsdeleted,0)
        );
    }


    //用户分页
    @Override
    public Page<User> getUserPage(String key,Integer pageNum, Integer pageSize) {
        Page<User> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(key)) {
            queryWrapper.like(User::getUserName, key);
        }
        queryWrapper.eq(User::getIsdeleted,0);
        queryWrapper.orderByDesc(User::getLoginDate);
        return this.page(page, queryWrapper);
    }

    @Override
    public void banUser(User user) {
        User exituser = this.isExistUser(user);

        if(exituser!=null){
            userMapper.update(user,new LambdaUpdateWrapper<User>()
                    .eq(User::getUserId,exituser.getUserId())
                    .set(User::getIsAction,0)
            );

        }

    }

    @Override
    public void unBanUser(User user) {

        User exituser = this.isExistUser(user);

        if(exituser!=null){
            userMapper.update(user,new LambdaUpdateWrapper<User>()
                    .eq(User::getUserId,exituser.getUserId())
                    .set(User::getIsAction,1)
            );

        }

    }

    @Override
    public void forgetPassword(User user) {
        //忘记密码
        userMapper.update(user,new LambdaUpdateWrapper<User>()
                .eq(User::getUserId,user.getUserId())
                .set(User::getIsRemember,1)
        );

    }

    @Override
    public String resetPassword(User user) {

        userMapper.update(user,new LambdaUpdateWrapper<User>()
                .eq(User::getUserId,user.getUserId())
                .set(User::getIsRemember,0)
        );
        return "success";
    }


}




